package com.zhixinhuixue.zsyte.xxx.ui.activity

import android.os.Bundle
import android.widget.CompoundButton
import com.zhixinhuixue.zsyte.xxx.R
import com.zhixinhuixue.zsyte.xxx.app.App
import com.zhixinhuixue.zsyte.xxx.app.api.NetUrl
import com.zhixinhuixue.zsyte.xxx.app.base.BaseActivity
import com.zhixinhuixue.zsyte.xxx.app.ext.initBack
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityLoginBinding
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.LoginViewModel
import me.hgj.mvvmhelper.ext.getStringExt
import me.hgj.mvvmhelper.ext.showDialogMessage
import me.hgj.mvvmhelper.ext.toStartActivity
import me.hgj.mvvmhelper.net.LoadStatusEntity
import me.hgj.mvvmhelper.util.SPUtil


/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　: 虽然在Activity代码少了，但是DataBinding 不太好用
 */
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        //初始化toolbar
        mToolbar.initBack(getStringExt(R.string.login_submit)) {
            finish()
        }
        mDataBind.viewModel = mViewModel
        mDataBind.click = LoginClickProxy()
    }

    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
        //监听登录结果
        mViewModel.loginData.observe(this, {
            SPUtil.put(
                App.instance.applicationContext,
                SPUtil.USER_INFO,
                it
            )
            jumpToMainActivity()
            finish()
        })
    }

    /**
     * 请求失败
     * @param loadStatus LoadStatusEntity
     */
    override fun onRequestError(loadStatus: LoadStatusEntity) {

        when (loadStatus.requestCode) {

            NetUrl.LOGIN -> {
                when (loadStatus.errorMessage) {
                    "NG" -> showDialogMessage("用户名或密码错误")
                    else -> showDialogMessage(loadStatus.errorMessage)
                }

            }
        }
    }

    inner class LoginClickProxy {

        fun clear() {
            mViewModel.userName.set("")
        }

        //登录
        fun login() {
            when {
                mViewModel.userName.get().isEmpty() -> showDialogMessage("账号不能为空")
                mViewModel.password.get().isEmpty() -> showDialogMessage("密码不能为空")
                else -> {
                    mViewModel.login(
                        mViewModel.userName.get(),
                        mViewModel.password.get()
                    )
                }
            }
        }

        fun setting() {
            toStartActivity(ConfigurationActivity::class.java)
        }

        var onCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowPwd.set(isChecked)
            }
    }


    private fun jumpToMainActivity() {
        toStartActivity(WLSJActivity::class.java)
        finish()
    }


}